{
    "cells": [
        {
            "cell_type": "markdown",
            "id": "36e7bb96-0c27-47e9-a525-c11f40be3b86",
            "metadata": {},
            "source": [
                "# Weaviate"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "38ca1434",
            "metadata": {},
            "outputs": [],
            "source": [
                "import logging\n",
                "import sys\n",
                "\n",
                "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
                "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "id": "d99bc57b-85df-46ac-8262-2409344af428",
            "metadata": {},
            "outputs": [],
            "source": [
                "import weaviate\n",
                "from llama_index.readers.weaviate import WeaviateReader"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "fec36c7a-3766-4167-890e-b93adb831a64",
            "metadata": {},
            "outputs": [],
            "source": [
                "# See https://weaviate.io/developers/weaviate/current/client-libraries/python.html\n",
                "# for more details on authentication\n",
                "resource_owner_config = weaviate.AuthClientPassword(\n",
                "  username = \"<username>\", \n",
                "  password = \"<password>\", \n",
                ")\n",
                "\n",
                "# initialize reader\n",
                "reader = WeaviateReader(\"https://<cluster-id>.semi.network/\", auth_client_secret=resource_owner_config)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "ce9f299c-4f0a-4bca-bc90-79848f02b381",
            "metadata": {},
            "source": [
                "You have two options for the Weaviate reader: 1) directly specify the class_name and properties, or 2) input the raw graphql_query. Examples are shown below."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "b92d69a1-d39f-45cf-a136-cb9c2f2f5cdf",
            "metadata": {},
            "outputs": [],
            "source": [
                "# 1) load data using class_name and properties\n",
                "# docs = reader.load_data(\n",
                "#    class_name=\"Author\", properties=[\"name\", \"description\"], separate_documents=True\n",
                "# )\n",
                "\n",
                "documents = reader.load_data(\n",
                "    class_name=\"<class_name>\", \n",
                "    properties=[\"property1\", \"property2\", \"...\"], \n",
                "    separate_documents=True\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 4,
            "id": "722b5d47-9897-4c54-9734-259ab0c1634c",
            "metadata": {},
            "outputs": [],
            "source": [
                "# 2) example GraphQL query\n",
                "# query = \"\"\"\n",
                "# {\n",
                "#   Get {\n",
                "#     Author {\n",
                "#       name\n",
                "#       description\n",
                "#     }\n",
                "#   }\n",
                "# }\n",
                "# \"\"\"\n",
                "# docs = reader.load_data(graphql_query=query, separate_documents=True)\n",
                "\n",
                "query = \"\"\"\n",
                "{\n",
                "  Get {\n",
                "    <class_name> {\n",
                "      <property1>\n",
                "      <property2>\n",
                "      ...\n",
                "    }\n",
                "  }\n",
                "}\n",
                "\"\"\"\n",
                "\n",
                "documents = reader.load_data(graphql_query=query, separate_documents=True)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "169b4273-eb20-4d06-9ffe-71320f4570f6",
            "metadata": {},
            "source": [
                "### Create index"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "92599a0a-93ba-4c93-80f1-9acae0663c34",
            "metadata": {},
            "outputs": [],
            "source": [
                "index = GPTListIndex.from_documents(documents)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "52d93c3f-a08d-4637-98bc-0c3cc693c563",
            "metadata": {},
            "outputs": [],
            "source": [
                "# set Logging to DEBUG for more detailed outputs\n",
                "response = index.query(\"<query_text>\")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "771b42be-4108-43a0-a1b4-b259a7819936",
            "metadata": {},
            "outputs": [],
            "source": [
                "display(Markdown(f\"<b>{response}</b>\"))"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3 (ipykernel)",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.11.1"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}